package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubChangesQuotaDao;
import com.game.server.db.dao.XyGameLogDao;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.message.proto.in.AbstractXYfindGameAllLogReq;
import com.game.server.message.proto.out.XYclubPlayerxinyuLogStruct;
import com.game.server.message.proto.out.XYfindGameAllLogRsp;
import com.game.server.message.proto.out.XYgameAlllogStruct;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class XYfindGameAllLogReq extends AbstractXYfindGameAllLogReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            int limit = 20;
            XYfindGameAllLogRsp rsp = new XYfindGameAllLogRsp();
            JSONObject json = XyGameLogDao.findPage(limit, this.page, this.clubid, client.getUserId(),this.type);
            rsp.setAllpage(json.getInt("allpage"));
            rsp.setAllcount(json.getInt("allcount"));
            rsp.setPage(this.page);
            rsp.setType(this.type);
            JSONArray array = json.getJSONArray("array");
            XYgameAlllogStruct[] gameAlllogs = new XYgameAlllogStruct[array.size()];
            int index = 0;
            for (Object ent : array) {
                JSONObject object = JSONObject.fromObject(ent);
                XYgameAlllogStruct alllog = new XYgameAlllogStruct();
                alllog.setTableid(object.getInt("tableid"));
                alllog.setCid(object.getInt("cid"));
                int clubid = object.getInt("clubid");
                alllog.setClubid(clubid);
                if (clubid!=0){
                    XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(clubid);
                    if (clubInfo!=null){
                        alllog.setName(clubInfo.getName());
                        alllog.setHead(clubInfo.getHeadurl());
                    }
                }else{
                    UserInfoData userInfoData = DataManager.getInstance().getMapUserData().get(alllog.getCid());
                    alllog.setName(userInfoData.getName());
                    alllog.setHead(userInfoData.getHead());
                }
                alllog.setPi(object.getInt("pi"));
                alllog.setGameduration(object.getInt("gametime"));
                alllog.setTablename(object.getString("tablename"));
                alllog.setTableonly(object.getString("tableonly"));
                alllog.setGameendtime(object.getString("gameendtime"));
                alllog.setGameenddate(object.getString("gameenddate"));
                String[] windairu = object.getString("windairu").split(",");
                alllog.setWin(Integer.parseInt(windairu[0]));
                alllog.setDairu(Integer.parseInt(windairu[1]));
                gameAlllogs[index] = alllog;
                index++;
            }
            rsp.setAmeAlllogs(gameAlllogs);
            client.Send(rsp);
        }
    }

}
